package cn.gjxblog.thread.example.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/1 16:01
 */
@Slf4j
public class ThreadPoolExample3 {

    public static void main(String[] args) {

        ExecutorService executorService =
                Executors.newScheduledThreadPool(5);

//      test1((ScheduledExecutorService) executorService);

        test2((ScheduledExecutorService) executorService);


    }


    private static void test2(ScheduledExecutorService executorService) {
        for(int i = 0; i < 100; i++){
            //newScheduledThreadPool是延时一定时间之后才执行
            executorService.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    log.info("thread name-{}",Thread.currentThread().getName());
                }
            },5,3,TimeUnit.SECONDS);
        }
    }

    private static void test1(ScheduledExecutorService executorService) {
        for(int i = 0; i < 100; i++){
            //newScheduledThreadPool是延时一定时间之后才执行
            executorService.schedule(new Runnable() {
                @Override
                public void run() {
                    log.info("thread name-{}",Thread.currentThread().getName());
                }
            },5,TimeUnit.SECONDS);
        }
    }
}
